package com.dlnu.sujiwords.service.Impl;

import com.dlnu.sujiwords.dao.EnWordsMapper;
import com.dlnu.sujiwords.dao.Query_historyMapper;
import com.dlnu.sujiwords.domain.client.AjaxResult;
import com.dlnu.sujiwords.domain.pojo.EnWords;
import com.dlnu.sujiwords.domain.client.SelTableData;
import com.dlnu.sujiwords.domain.external.WordQuestion;
import com.dlnu.sujiwords.domain.pojo.Query_history;
import com.dlnu.sujiwords.service.IEnWordsService;
import com.dlnu.sujiwords.util.constant.EnWordsConstant;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@Service
public class EnWordsServiceImpl implements IEnWordsService {
    @Resource
    private EnWordsMapper enWordsMappermapper;
    @Resource
    private Query_historyMapper queryHistoryMapper;

    /*获取指定数量的随机单词*/
    @Override
    public List<EnWords> extractWords(int extractNum) {
        List<EnWords> result = enWordsMappermapper.spotCheck(getExtract(extractNum));
        return result;
    }

    /*英译汉*/
    @Override
    public AjaxResult EnToCh(String en) {
        EnWords res = enWordsMappermapper.EnToCh(en);
        if (res != null) {
            return AjaxResult.success("查找成功", res);
        } else {
            return AjaxResult.success("找不到该单词,请检查拼写");
        }
    }

    /*汉译英*/
    @Override
    public AjaxResult ChToEn(String cn) {
        List<EnWords> resList = enWordsMappermapper.ChToEn(cn);
        if (resList.size() == 0) {
            return AjaxResult.success("无法找到该单词");
        } else if (resList.size() == 1) {
            return AjaxResult.success("成功查找", resList);
        } else {

            List<EnWords> res = new ArrayList<>();
            for (int i = 0; i < resList.size(); i++) {
                if(filtration(resList.get(i),cn)){
                    res.add(resList.get(i));
                }
            }
            System.out.println(resList.size());
            System.out.println(res.size());
            return AjaxResult.success("成功查找", res);
        }
    }


    @Override
    public AjaxResult selTableWords(int table, int pageNum, int pageSize) {
        int length;
        List<EnWords> tableData;
        PageHelper.startPage(pageNum, pageSize);
        switch (table) {
            case 0:
                tableData = enWordsMappermapper.selSchools();
                length = enWordsMappermapper.selSchoolsTotal();
                break;
            case 1:
                tableData = enWordsMappermapper.selHigh_school();
                length = enWordsMappermapper.selHigh_schoolTotal();
                break;
            case 2:
                tableData = enWordsMappermapper.selCET_4();
                length = enWordsMappermapper.selCET_4Total();
                break;
            case 3:
                tableData = enWordsMappermapper.selCET_6();
                length = enWordsMappermapper.selCET_6Total();
                break;
            case 4:
                tableData = enWordsMappermapper.selPostgraduata_word();
                length = enWordsMappermapper.selPostgraduata_wordTotal();
                break;
            default:
                tableData = enWordsMappermapper.selEnwords();
                length = enWordsMappermapper.selEnwordsTotal();
                break;
        }

        PageInfo<EnWords> enWordsPageInfo = new PageInfo<>(tableData);
        List<EnWords> res = enWordsPageInfo.getList();
        return AjaxResult.success("成功查询", new SelTableData(res, length));
    }

    @Override
    public AjaxResult searchWords(EnWords search, int table, int pageNum, int pageSize) {
        int length;
        List<EnWords> tableData;
        PageHelper.startPage(pageNum, pageSize);
        switch (table) {
            case 0:
                tableData = enWordsMappermapper.searchWordFromSchools(search);
                length = enWordsMappermapper.selSchoolsTotal();
                break;
            case 1:
                tableData = enWordsMappermapper.searchWordFromHigh_school(search);
                length = enWordsMappermapper.selHigh_schoolTotal();
                break;
            case 2:
                tableData = enWordsMappermapper.searchWordFromCET_4(search);
                length = enWordsMappermapper.selCET_4Total();
                break;
            case 3:
                tableData = enWordsMappermapper.searchWordFromCET_6(search);
                length = enWordsMappermapper.selCET_6Total();
                break;
            case 4:
                tableData = enWordsMappermapper.searchWordFromPostgraduata_word(search);
                length = enWordsMappermapper.selPostgraduata_wordTotal();
                break;
            default:
                tableData = enWordsMappermapper.searchWordFromEnwords(search);
                length = enWordsMappermapper.selEnwordsTotal();
                break;
        }

        PageInfo<EnWords> enWordsPageInfo = new PageInfo<>(tableData);
        List<EnWords> res = enWordsPageInfo.getList();
        return AjaxResult.success("成功查询", new SelTableData(res, length));
    }

    /*按词本背单词*/
    @Override
    public AjaxResult remberWords(int table, int hasRememberWordsNum,int willRememberWordsNum) {
        List<EnWords> rememberWordsList = new ArrayList<>();//记忆单词列表
        List<WordQuestion> wordQuestionList = new ArrayList<>();//单词问题列表
        try {
            for (int i = 0; i < willRememberWordsNum; i++) {
                rememberWordsList.add(getTableWord(table,hasRememberWordsNum+i+1));
            }
            for (int i = 0; i < willRememberWordsNum; i++) {
                EnWords word = rememberWordsList.get(i);
                List<EnWords> wrongAnswers = createWrongAnswer(3);
                wordQuestionList.add(new WordQuestion(word,wrongAnswers));
            }
            return AjaxResult.success("获取成功",wordQuestionList);
        }catch (Exception e){
            return AjaxResult.success("无法获取更多单词,你可能已经背完或出现bug请联系管理员");
        }

    }
    /*创建错误词汇列表*/
public  List<EnWords> createWrongAnswer(int num){
        Random random = new Random();
        List<EnWords> wrongList = new ArrayList<>();
        for (int i = 0; i < num; i++) {
            String translation = enWordsMappermapper.getWordByIdFromEnwords(random.nextInt(EnWordsConstant.WORDS_NUMS-1)+1).getTranslation();
            String word = enWordsMappermapper.getWordByIdFromEnwords(random.nextInt(EnWordsConstant.WORDS_NUMS-1)+1).getWord();
            if(translation.length()>20||word.length()>20){
                i--;
                continue;
            }
            wrongList.add(new EnWords(word,translation));
        }
        return wrongList;
}
    /*筛选匹配的汉字*/
    public boolean filtration(EnWords en,String cn){
        String selTran = en.getTranslation();
        int strPosition = selTran.indexOf(cn); /*出现匹配中文的位置*/
//        System.out.println(selTran);
        if(strPosition != 0){
            if (!(selTran.charAt(strPosition-1)>0 && selTran.charAt(strPosition-1)<255)){
                return false;
            }
        }
        if(!(strPosition+cn.length() >= selTran.length())){
            if(!(selTran.charAt(strPosition+cn.length())>0 && selTran.charAt(strPosition+cn.length())<255)){
                return false;
            }
        }
        return true;
    }

    /*获取单词*/
    public  EnWords getTableWord(int table,int wordId){
        EnWords res;
        switch (table){
            /*table为0表示中小学*/
            case 0:
                res = enWordsMappermapper.getWordByIdFromSchools(wordId);
                break;
            /*table为1表示高中*/
            case 1:
                res = enWordsMappermapper.getWordByIdFromHigh_school(wordId);
                break;
            /*table为2表示CET_4*/
            case 2:
                res = enWordsMappermapper.getWordByIdFromCET_4(wordId);
                break;
            case 3:
                res = enWordsMappermapper.getWordByIdFromCET_6(wordId);
                break;
            case 4:
                res = enWordsMappermapper.getWordByIdFromPostgraduata_word(wordId);
                break;
            default:
                res = enWordsMappermapper.getWordByIdFromEnwords(wordId);
        }

        return res;
    }

    /*找到最匹配的翻译*/
    public List<EnWords> bestMatch(List<EnWords> resList, String cn) {
        EnWords ObjRes = resList.get(0);
        List<EnWords> enWordsList = new ArrayList<>();

        int Objlen = MatchLength(ObjRes, cn);
        for (int i = 0; i < resList.size(); i++) {
            int reslistlen = MatchLength(resList.get(i), cn);

            if ((Objlen > reslistlen)) {
                ObjRes = resList.get(i);
                Objlen = MatchLength(ObjRes, cn);
                enWordsList.add(ObjRes);
            } else if ((cn.length() / Objlen == (cn.length() / reslistlen))) {
                if (translationLengtg(ObjRes) <= translationLengtg(resList.get(i))) {
                    ObjRes = resList.get(i);
                    Objlen = MatchLength(ObjRes, cn);
                    enWordsList.add(ObjRes);
                }
            }
        }

        return enWordsList;
    }
    /*匹配长度,需要翻译的语句在获取的对象翻译中的占比*/
    /*匹配长度*/
    public int MatchLength(EnWords en, String cn) {
        int res = 0;
        boolean count = false;
        String check = en.getTranslation();
        for (int i = 0; i < check.length(); i++) {
            if (check.charAt(i) == cn.charAt(0)) {
                for (int z = 0; z < cn.length(); z++) {
                    count = true;
                    if (check.charAt(i + z) != cn.charAt(z)) {
                        count = false;
                        break;
                    }
                }
            }
            if (count == true) {
                if (check.charAt(i) >= 0 && check.charAt(i) <= 126) {
                    break;
                } else {
                    res++;
                }
            }
        }

        return res;
    }

    /*翻译长度*/
    public int translationLengtg(EnWords en) {
        String translation = en.getTranslation();
        int res = translation.length();
        for (int i = 0; i < translation.length(); i++) {
            if (translation.charAt(i) >= 0 || translation.charAt(i) <= 126) {
                res -= 1;
            }
        }
        return res;
    }

    /*获取随机单词id*/
    public List<Integer> getExtract(int extractNum) {
        Random r = new Random();
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < extractNum; i++) {
            list.add(r.nextInt(EnWordsConstant.WORDS_NUMS + 1));
        }
        return list;
    }
}
